﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using BoxGame;

namespace BoxGame.Interface
{
    class Chatbox
    {
        String parsedText;
        int transparenz;
        Rectangle chatRechteck;
        Game1 game1;
        Texture2D chatBoxTexture;
        GraphicsDevice device;
        const int abstandLinks = 3;
        const int abstandUnten = 185;
        SpriteFont font;

        public Chatbox(Game1 mgame, GraphicsDevice mdevice)
        {
            game1 = mgame;
            device = mdevice;
            chatRechteck = new Rectangle(abstandLinks, device.Viewport.Height - abstandUnten, 270, 175);
            chatBoxTexture = game1.Content.Load<Texture2D>(@"Gui\Chatbox");
            font = font = game1.Content.Load<SpriteFont>("Spritefont1"); ;
            // Zum Testen.
            parsedText = parsen("Hello World. Oh Oh das koennte ein bissi zu lang sein!");
        }

        public void posUpdaten()
        {
            chatRechteck.X = abstandLinks;
            chatRechteck.Y = device.Viewport.Height - abstandUnten;
        }

        public Rectangle rechteckGeben()
        {
            return chatRechteck;
        }

        public Vector2 posAlsVectorGeben()
        {
            return new Vector2(chatRechteck.X + 8, chatRechteck.Y + 3);
        }

        public Texture2D textureGeben()
        {
            return chatBoxTexture;
        }

        public void groesseSetzen(int x, int y)
        {
            chatRechteck.Width = x;
            chatRechteck.Height = y;
        }

        private String parsen(String mText)
        {
            String line = String.Empty;
            String returnString = String.Empty;
            String[] wordArray = mText.Split(' ');

            foreach (String word in wordArray)
            {
                if (font.MeasureString(line + word).Length() > chatRechteck.Width)
                {
                    returnString = returnString + line + '\n';
                    line = String.Empty;
                }

                line = line + word + ' ';
            }

            return returnString + line;
        }

        public void schreiben(String neuerText)
        {
            parsedText = parsen(neuerText + parsedText);
        }

        public String textGeben()
        {
            return parsedText;
        }

        public void malen()
        {
            // Chatbox Hintergrund malen
            game1.spriteBatch.Draw(chatBoxTexture, chatRechteck, null, Color.White, 0.0f, new Vector2(0f, 0f), SpriteEffects.None, 0.6f);
            // Chatbox Text malen
            game1.spriteBatch.DrawString(font, parsedText, this.posAlsVectorGeben(), Color.White);
        }
    }
}
